package com.qishon.pc.domain.service;

import com.qishon.pc.domain.util.GirardEnum;
import com.qishon.pc.domain.util.UploadDatabase;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * Created by yongwei.chen on 2017/1/4.
 */
@Component
@Slf4j
@RabbitListener(queues = "databaseReceiver")
public class DataBaseReceiverService {
    @Value("${run.path}")
    private String runPath;

    @Autowired
    private ProgressStepsService progressStepsSerivce;

    @Autowired
    private UploadDatabase uploadDatabase;

    @RabbitHandler
    public void process(String msg) {
        log.info("databaseReceiver:" + msg);
        //获取msg字符串
        String [] strings=msg.split(",");
        String uploadName = GirardEnum.findByKey(Integer.parseInt(strings[0])).getFilePtah();
        String fromName = GirardEnum.findByKey(Integer.parseInt(strings[0])-1).getFilePtah();
        String path=runPath+"\\"+fromName+"\\"+strings[2];
        String upPath=runPath+"\\"+uploadName+"\\"+strings[2];
        //文件拷贝
        boolean result = progressStepsSerivce.copyDirectory(path,upPath,true);
        if (!result){
            log.info("Copy directory failed in 07.Database!");
        }
        try {
            uploadDatabase.FileUpload(path,upPath,strings[2]);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
